<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>rangemodel.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_rangemodel.js.html">rangemodel.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Implementation of a range model. This is an implementation of
<a name="line17"></a> * the BoundedRangeModel as described by Java at
<a name="line18"></a> * http://java.sun.com/javase/6/docs/api/javax/swing/BoundedRangeModel.html.
<a name="line19"></a> *
<a name="line20"></a> * One good way to understand the range model is to think of a scroll bar for
<a name="line21"></a> * a scrollable element. In that case minimum is 0, maximum is scrollHeight,
<a name="line22"></a> * value is scrollTop and extent is clientHeight.
<a name="line23"></a> *
<a name="line24"></a> * Based on http://webfx.eae.net/dhtml/slider/js/range.js
<a name="line25"></a> *
<a name="line26"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line27"></a> */
<a name="line28"></a>
<a name="line29"></a>goog.provide(&#39;goog.ui.RangeModel&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Creates a range model
<a name="line38"></a> * @extends {goog.events.EventTarget}
<a name="line39"></a> * @constructor
<a name="line40"></a> */
<a name="line41"></a>goog.ui.RangeModel = function() {
<a name="line42"></a>  goog.events.EventTarget.call(this);
<a name="line43"></a>};
<a name="line44"></a>goog.inherits(goog.ui.RangeModel, goog.events.EventTarget);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * @type {number}
<a name="line49"></a> * @private
<a name="line50"></a> */
<a name="line51"></a>goog.ui.RangeModel.prototype.value_ = 0;
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * @type {number}
<a name="line56"></a> * @private
<a name="line57"></a> */
<a name="line58"></a>goog.ui.RangeModel.prototype.minimum_ = 0;
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * @type {number}
<a name="line63"></a> * @private
<a name="line64"></a> */
<a name="line65"></a>goog.ui.RangeModel.prototype.maximum_ = 100;
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * @type {number}
<a name="line70"></a> * @private
<a name="line71"></a> */
<a name="line72"></a>goog.ui.RangeModel.prototype.extent_ = 0;
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * @type {?number}
<a name="line77"></a> * @private
<a name="line78"></a> */
<a name="line79"></a>goog.ui.RangeModel.prototype.step_ = 1;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * This is true if something is changed as a side effect. This happens when for
<a name="line84"></a> * example we set the maximum below the current value.
<a name="line85"></a> * @type {boolean}
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.ui.RangeModel.prototype.isChanging_ = false;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * If set to true, we do not fire any change events.
<a name="line93"></a> * @type {boolean}
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.ui.RangeModel.prototype.mute_ = false;
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Sets the model to mute / unmute.
<a name="line101"></a> * @param {boolean} muteValue Whether or not to mute the range, i.e.,
<a name="line102"></a> *     suppress any CHANGE events.
<a name="line103"></a> */
<a name="line104"></a>goog.ui.RangeModel.prototype.setMute = function(muteValue) {
<a name="line105"></a>  this.mute_ = muteValue;
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Sets the value.
<a name="line111"></a> * @param {number} value The new value.
<a name="line112"></a> */
<a name="line113"></a>goog.ui.RangeModel.prototype.setValue = function(value) {
<a name="line114"></a>  value = this.roundToStepWithMin(value);
<a name="line115"></a>  if (this.value_ != value) {
<a name="line116"></a>    if (value + this.extent_ &gt; this.maximum_) {
<a name="line117"></a>      this.value_ = this.maximum_ - this.extent_;
<a name="line118"></a>    } else if (value &lt; this.minimum_) {
<a name="line119"></a>      this.value_ = this.minimum_;
<a name="line120"></a>    } else {
<a name="line121"></a>      this.value_ = value;
<a name="line122"></a>    }
<a name="line123"></a>    if (!this.isChanging_ &amp;&amp; !this.mute_) {
<a name="line124"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line125"></a>    }
<a name="line126"></a>  }
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * @return {number} the current value.
<a name="line132"></a> */
<a name="line133"></a>goog.ui.RangeModel.prototype.getValue = function() {
<a name="line134"></a>  return this.roundToStepWithMin(this.value_);
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Sets the extent. The extent is the &#39;size&#39; of the value.
<a name="line140"></a> * @param {number} extent The new extent.
<a name="line141"></a> */
<a name="line142"></a>goog.ui.RangeModel.prototype.setExtent = function(extent) {
<a name="line143"></a>  extent = this.roundToStepWithMin(extent);
<a name="line144"></a>  if (this.extent_ != extent) {
<a name="line145"></a>    if (extent &lt; 0) {
<a name="line146"></a>      this.extent_ = 0;
<a name="line147"></a>    } else if (this.value_ + extent &gt; this.maximum_) {
<a name="line148"></a>      this.extent_ = this.maximum_ - this.value_;
<a name="line149"></a>    } else {
<a name="line150"></a>      this.extent_ = extent;
<a name="line151"></a>    }
<a name="line152"></a>    if (!this.isChanging_ &amp;&amp; !this.mute_) {
<a name="line153"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line154"></a>    }
<a name="line155"></a>  }
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * @return {number} The extent for the range model.
<a name="line161"></a> */
<a name="line162"></a>goog.ui.RangeModel.prototype.getExtent = function() {
<a name="line163"></a>  return this.roundToStep(this.extent_);
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Sets the minimum
<a name="line169"></a> * @param {number} minimum The new minimum.
<a name="line170"></a> */
<a name="line171"></a>goog.ui.RangeModel.prototype.setMinimum = function(minimum) {
<a name="line172"></a>  // Don&#39;t round minimum because it is the base
<a name="line173"></a>  if (this.minimum_ != minimum) {
<a name="line174"></a>    var oldIsChanging = this.isChanging_;
<a name="line175"></a>    this.isChanging_ = true;
<a name="line176"></a>
<a name="line177"></a>    this.minimum_ = minimum;
<a name="line178"></a>
<a name="line179"></a>    if (minimum + this.extent_ &gt; this.maximum_) {
<a name="line180"></a>      this.extent_ = this.maximum_ - this.minimum_;
<a name="line181"></a>    }
<a name="line182"></a>    if (minimum &gt; this.value_) {
<a name="line183"></a>      this.setValue(minimum);
<a name="line184"></a>    }
<a name="line185"></a>    if (minimum &gt; this.maximum_) {
<a name="line186"></a>      this.extent_ = 0;
<a name="line187"></a>      this.setMaximum(minimum);
<a name="line188"></a>      this.setValue(minimum);
<a name="line189"></a>    }
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>    this.isChanging_ = oldIsChanging;
<a name="line193"></a>    if (!this.isChanging_ &amp;&amp; !this.mute_) {
<a name="line194"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line195"></a>    }
<a name="line196"></a>  }
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * @return {number} The minimum value for the range model.
<a name="line202"></a> */
<a name="line203"></a>goog.ui.RangeModel.prototype.getMinimum = function() {
<a name="line204"></a>  return this.roundToStepWithMin(this.minimum_);
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Sets the maximum
<a name="line210"></a> * @param {number} maximum The new maximum.
<a name="line211"></a> */
<a name="line212"></a>goog.ui.RangeModel.prototype.setMaximum = function(maximum) {
<a name="line213"></a>  maximum = this.roundToStepWithMin(maximum);
<a name="line214"></a>  if (this.maximum_ != maximum) {
<a name="line215"></a>    var oldIsChanging = this.isChanging_;
<a name="line216"></a>    this.isChanging_ = true;
<a name="line217"></a>
<a name="line218"></a>    this.maximum_ = maximum;
<a name="line219"></a>
<a name="line220"></a>    if (maximum &lt; this.value_ + this.extent_) {
<a name="line221"></a>      this.setValue(maximum - this.extent_);
<a name="line222"></a>    }
<a name="line223"></a>    if (maximum &lt; this.minimum_) {
<a name="line224"></a>      this.extent_ = 0;
<a name="line225"></a>      this.setMinimum(maximum);
<a name="line226"></a>      this.setValue(this.maximum_);
<a name="line227"></a>    }
<a name="line228"></a>    if (maximum &lt; this.minimum_ + this.extent_) {
<a name="line229"></a>      this.extent_ = this.maximum_ - this.minimum_;
<a name="line230"></a>    }
<a name="line231"></a>
<a name="line232"></a>    this.isChanging_ = oldIsChanging;
<a name="line233"></a>    if (!this.isChanging_ &amp;&amp; !this.mute_) {
<a name="line234"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line235"></a>    }
<a name="line236"></a>  }
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * @return {number} The maximimum value for the range model.
<a name="line242"></a> */
<a name="line243"></a>goog.ui.RangeModel.prototype.getMaximum = function() {
<a name="line244"></a>  return this.roundToStepWithMin(this.maximum_);
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * Returns the step value. The step value is used to determine how to round the
<a name="line250"></a> * value.
<a name="line251"></a> * @return {?number} The maximimum value for the range model.
<a name="line252"></a> */
<a name="line253"></a>goog.ui.RangeModel.prototype.getStep = function() {
<a name="line254"></a>  return this.step_;
<a name="line255"></a>};
<a name="line256"></a>
<a name="line257"></a>
<a name="line258"></a>/**
<a name="line259"></a> * Sets the step. The step value is used to determine how to round the value.
<a name="line260"></a> * @param {?number} step  The step size.
<a name="line261"></a> */
<a name="line262"></a>goog.ui.RangeModel.prototype.setStep = function(step) {
<a name="line263"></a>  if (this.step_ != step) {
<a name="line264"></a>    this.step_ = step;
<a name="line265"></a>
<a name="line266"></a>    // adjust value, extent and maximum
<a name="line267"></a>    var oldIsChanging = this.isChanging_;
<a name="line268"></a>    this.isChanging_ = true;
<a name="line269"></a>
<a name="line270"></a>    this.setMaximum(this.getMaximum());
<a name="line271"></a>    this.setExtent(this.getExtent());
<a name="line272"></a>    this.setValue(this.getValue());
<a name="line273"></a>
<a name="line274"></a>    this.isChanging_ = oldIsChanging;
<a name="line275"></a>    if (!this.isChanging_ &amp;&amp; !this.mute_) {
<a name="line276"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line277"></a>    }
<a name="line278"></a>  }
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Rounds to the closest step using the minimum value as the base.
<a name="line284"></a> * @param {number} value  The number to round.
<a name="line285"></a> * @return {number} The number rounded to the closest step.
<a name="line286"></a> */
<a name="line287"></a>goog.ui.RangeModel.prototype.roundToStepWithMin = function(value) {
<a name="line288"></a>  if (this.step_ == null) return value;
<a name="line289"></a>  return this.minimum_ +
<a name="line290"></a>      Math.round((value - this.minimum_) / this.step_) * this.step_;
<a name="line291"></a>};
<a name="line292"></a>
<a name="line293"></a>
<a name="line294"></a>/**
<a name="line295"></a> * Rounds to the closest step.
<a name="line296"></a> * @param {number} value  The number to round.
<a name="line297"></a> * @return {number} The number rounded to the closest step.
<a name="line298"></a> */
<a name="line299"></a>goog.ui.RangeModel.prototype.roundToStep = function(value) {
<a name="line300"></a>  if (this.step_ == null) return value;
<a name="line301"></a>  return Math.round(value / this.step_) * this.step_;
<a name="line302"></a>};
</pre>


</body>
</html>
